package com.sky.loveshop.service.impl;

import com.sky.loveshop.api.Apis;
import com.sky.loveshop.api.CollectMoneyOrder;
import com.sky.loveshop.entity.collectMoney.ActivateResponse;
import com.sky.loveshop.service.CollectMoneyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/9/24 22:44
 */
@Slf4j
@Service
public class CollectMoneyServiceImpl implements CollectMoneyService {

    @Value("${shou_qian_ba.vendor_sn}")
    private String vendorSn;
    @Value("${shou_qian_ba.vendor_key}")
    private String vendorKey;
    @Value("${shou_qian_ba.app_id}")
    private String appId;
    @Value("${shou_qian_ba.code}")
    private String code;

    public ActivateResponse response = null;

    @Override
    public ActivateResponse activate(CollectMoneyOrder.ActivateRequest request) {

        JSONObject params = new JSONObject();

        if (null != response) {
            return response;
        }

        try {
            params.put("app_id", appId);                       //app_id，必填
            params.put("code", code);                          //激活码，必填
            params.put("device_id", request.getDeviceId());    //客户方收银终端序列号，需保证同一app_id下唯一，必填。为方便识别，建议格式为“品牌名+门店编号+‘POS’+POS编号“
            params.put("client_sn", request.getClientSn());    //客户方终端编号，一般客户方或系统给收银终端的编号，必填
            params.put("name", request.getName());             //客户方终端名称，必填
            params.put("os_info", request.getOsInfo());        //当前系统信息
            params.put("sdk_version", request.getSdkVersion());//SDK版本

            String requestStr = params.toString() + vendorKey;
            String sign = DigestUtils.md5Hex(requestStr.getBytes("UTF-8"));

            response = Apis.SHOU_QIAN_BA_API.terminalActivate(request, vendorSn + " " + sign);
            if (!response.isSuccess()) {
                log.info("TerminalActivate not success");
                return null;
            }
            ActivateResponse.BizResponseDTO bizResponse = response.getBizResponse();
            if (bizResponse.getTerminalKey() == null || bizResponse.getTerminalSn() == null) {
                return null;
            }
            return response;
        } catch (Exception e) {
            log.error("TerminalActivate error 终端激活报错", e);
        }
        return null;
    }

    @Override
    public void testPreCreate(HttpServletResponse response) {

        CollectMoneyOrder.PreCreateRequest request = new CollectMoneyOrder.PreCreateRequest();

        JSONObject params = new JSONObject();
        //params.put("terminal_sn",terminal_sn);           //收钱吧终端ID
        //params.put("client_sn",getClient_Sn(16));  //商户系统订单号,必须在商户系统内唯一；且长度不超过32字节
        //params.put("total_amount","1000");               //交易总金额
        //params.put("payway","1");	                     //支付方式
        //params.put("subject","Pizza");	                 //交易简介
        //params.put("operator","Kay");	                 //门店操作员
        //params.put("sub_payway","3");	                 //二级支付方式

        //CollectMoneyTokenUtils.getToken();

        Apis.SHOU_QIAN_BA_API.preCreateOrder(request, "");

    }

}
